package com.sanjiahantang.youjiaclient.module.goods.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseApplication;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.module.goods.views.impl.GoodsDetailActivity;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchBean;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/6/19.
 */

public class GoodsListMoreAdapter extends RecyclerView.Adapter<GoodsListMoreAdapter.GoodsListHolder> implements StaticValue{

    private List<MatchBean.Goods> data;
    private Context context;
    private String type,typeGoodsId,findsimilar;

    public GoodsListMoreAdapter(Context context, List<MatchBean.Goods> data, String type,String typeGoodsId,String findsimilar) {
        this.context = context;
        this.data = data;
        this.type = type;
        this.typeGoodsId=typeGoodsId;
        this.findsimilar=findsimilar;
    }

    @Override
    public GoodsListHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_goods, parent, false);
            GoodsListHolder holder = new GoodsListHolder(view);
            return holder;


    }

    @Override
    public void onBindViewHolder(GoodsListHolder holder, final int position) {
        Picasso.with(context).load(FileUtils.rePath(data.get(position).getUrl()))
                .resize(150,150)
                .centerCrop()
                .placeholder(RES_ID_GOODS_DEFAULT).into(holder.iv_goodsIcon);
        holder.tv_goodsInfo.setText(data.get(position).getTitle());

        if (data.get(position).getIsSale() == 1){
            holder.isSale.setVisibility(View.VISIBLE);
            holder.tv_nowPrice.setText(String.format("¥ %.2f",data.get(position).getSalePrice()));
        }else{
            holder.isSale.setVisibility(View.GONE);
            holder.tv_nowPrice.setText(String.format("¥ %.2f", data.get(position).getBasePrice()));
        }
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsDetailActivity.class);
                intent.putExtra("goodsId", data.get(position).getMgiId());
                intent.putExtra("Add", typeGoodsId);
                intent.putExtra("FIND",findsimilar);
                BaseApplication.addActivity((Activity) context);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class GoodsListHolder extends RecyclerView.ViewHolder {

        ImageView iv_goodsIcon;
        TextView tv_goodsInfo;
        TextView tv_nowPrice;
        TextView isSale;
        View view;

        public GoodsListHolder(View itemView) {
            super(itemView);
            view = itemView;
            iv_goodsIcon = (ImageView) itemView.findViewById(R.id.iv_goodsIcon);
            tv_goodsInfo = (TextView) itemView.findViewById(R.id.tv_goodsInfo);
            tv_nowPrice = (TextView) itemView.findViewById(R.id.tv_nowPrice);
            isSale = (TextView) itemView.findViewById(R.id.isSale);
        }
    }
    public void addAll(List<MatchBean.Goods> elem) {
        data.addAll(elem);
    }



}
